/**
 * 
 */
package slhs.tempo.scriptGenerator;

import java.awt.LayoutManager;
import java.awt.event.ActionEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;

import org.mlc.swing.layout.LayoutConstraintsManager;

import slhs.tempo.TableAnalyzer.IntegerField;
import slhs.tempo.dataImporterUtilities.IOFileHandler;

/**
 * ImageFeedbackPanel.java
 * 
 * @author Swapnil Devikar
 * 
 */
public class ImageFeedbackPanel extends JPanel implements PanelHandler {

    private static final ImageFeedbackPanel imageFeedbackPanel = new ImageFeedbackPanel();

    private static final String IMAGE_FEEDBACK_OPTIONS_PANEL_LAYOUT = "IMAGE_FEEDBACK_OPTIONS_PANEL_LAYOUT.XML";

    private static final String IMAGE_FEEDBACK = "3";

    private JPanel imageFeedbackOptionsPanel;

    private JTextField wrongAnswerFeedbackImageTextField;
    private JTextField correctAnswerFeedbackImageTextField;
    private JTextField durationTextField;

    private JButton wrongAnswerFeedbackImageChoosebutton;
    private JButton correctAnswerFeedbackImageChoosebutton;

    private JLabel correctAnswerFeedbackImageLabel;
    private JLabel durationLabel;
    private JLabel wrongAnswerFeedbackImageLabel;

    private ImageFeedbackPanel() {
	initComponents();
	addComponents();
    }

    /**
     * 
     */
    private void initComponents() {
	// here are declarations for the controls you created
	imageFeedbackOptionsPanel = new JPanel();
	wrongAnswerFeedbackImageTextField = new JTextField();
	correctAnswerFeedbackImageLabel = new JLabel("Correct Answer Feedback Image");
	wrongAnswerFeedbackImageChoosebutton = new JButton("Choose");
	durationLabel = new JLabel("Duration On Screen (sec.)");
	correctAnswerFeedbackImageTextField = new JTextField();
	wrongAnswerFeedbackImageLabel = new JLabel("Wrong Answer Feedback Image");
	correctAnswerFeedbackImageChoosebutton = new JButton("Choose");
	durationTextField = new IntegerField();
    }

    /**
     * 
     */
    private void addComponents() {
	LayoutConstraintsManager layoutConstraintsManager = LayoutConstraintsManager.getLayoutConstraintsManager(this
		.getClass().getResourceAsStream(IMAGE_FEEDBACK_OPTIONS_PANEL_LAYOUT));
	LayoutManager panelLayout = layoutConstraintsManager.createLayout("panel", this);
	imageFeedbackOptionsPanel.setLayout(panelLayout);

	imageFeedbackOptionsPanel.add(wrongAnswerFeedbackImageTextField, "wrongAnswerFeedbackImageTextField");
	imageFeedbackOptionsPanel.add(correctAnswerFeedbackImageLabel, "correctAnswerFeedbackImageLabel");
	imageFeedbackOptionsPanel.add(wrongAnswerFeedbackImageChoosebutton, "wrongAnswerFeedbackImageChoosebutton");
	imageFeedbackOptionsPanel.add(durationLabel, "durationLabel");
	imageFeedbackOptionsPanel.add(correctAnswerFeedbackImageTextField, "correctAnswerFeedbackImage");
	imageFeedbackOptionsPanel.add(wrongAnswerFeedbackImageLabel, "wrongAnswerFeedbackImageLabel");
	imageFeedbackOptionsPanel.add(correctAnswerFeedbackImageChoosebutton, "correctAnswerFeedbackImageChoosebutton");
	imageFeedbackOptionsPanel.add(durationTextField, "durationTextField");

	// actionlisteners
	correctAnswerFeedbackImageChoosebutton.addActionListener(this);
	wrongAnswerFeedbackImageChoosebutton.addActionListener(this);

    }

    /*
     * (non-Javadoc)
     * 
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    @Override
    public void actionPerformed(ActionEvent event) {

	Object source = event.getSource();
	FileNameExtensionFilter filter = new FileNameExtensionFilter("Image Files", "bmp", "jpg", "jpeg", "tiff",
		"gif", "png");
	String[] filePath = new IOFileHandler().selectFile(true, filter);
	if (source.equals(correctAnswerFeedbackImageChoosebutton)) {
	    correctAnswerFeedbackImageTextField.setText(filePath[0]);
	} else if (source.equals(wrongAnswerFeedbackImageChoosebutton)) {
	    wrongAnswerFeedbackImageTextField.setText(filePath[0]);
	}
    }

    /*
     * (non-Javadoc)
     * 
     * @see slhs.tempo.scriptGenerator.PanelHandler#getPanel()
     */
    @Override
    public JPanel getPanel() {
	return imageFeedbackOptionsPanel;
    }

    /**
     * @return
     */
    public static ImageFeedbackPanel getInstance() {
	return imageFeedbackPanel;
    }

    public String getFeedbackString() {
	StringBuffer imageFeedbackString = new StringBuffer();

	// 3 for image feedback
	imageFeedbackString.append(IMAGE_FEEDBACK);
	imageFeedbackString.append(UtilityConstants.VALUES_DELIMITER);

	// Correct Answer Image Feedback
	imageFeedbackString.append(getCorrectanswerImagePath());
	imageFeedbackString.append(UtilityConstants.VALUES_DELIMITER);

	// Wrong Answer Image Feedback
	imageFeedbackString.append(getWrongAnswerImagePath());
	imageFeedbackString.append(UtilityConstants.VALUES_DELIMITER);

	// Feedback Duration in Seconds
	imageFeedbackString.append(getDuration());

	return imageFeedbackString.toString();
    }

    /**
     * @return
     */
    private String getDuration() {
	return durationTextField.getText();
    }

    /**
     * @return
     */
    private String getWrongAnswerImagePath() {
	return wrongAnswerFeedbackImageTextField.getText();
    }

    /**
     * @return
     */
    private String getCorrectanswerImagePath() {
	return correctAnswerFeedbackImageTextField.getText();
    }
}
